package cn.kgc.controller;

import cn.kgc.pojo.Air_Quility_Index;
import cn.kgc.pojo.District;
import cn.kgc.service.AirService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author 许京东
 * @Description
 * @date 2021/6/2 10:06
 */
@Controller
public class AirController {
    @Autowired
    AirService airService;

    @RequestMapping("all")
    public String show(Model model,
                       @RequestParam(value = "did", required = false) Integer did,
                       @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex) {
        PageHelper.startPage(pageIndex, 3);
        model.addAttribute("did", did);
        List<Air_Quility_Index> air_quility_indices = airService.selectAllAir();
        PageInfo<Air_Quility_Index> pageInfo = new PageInfo<>(air_quility_indices);
        if (pageInfo == null) {
            model.addAttribute("error", "抱歉!暂无数据");
        }
        model.addAttribute("pageInfo", pageInfo);
        List<District> districts = airService.selectAllDistrict();
        model.addAttribute("districts", districts);
        return "index";
    }

    @RequestMapping("toadd")
    public String toadd(Model model) {
        List<District> districts = airService.selectAllDistrict();
        model.addAttribute("districts", districts);
        return "adds";
    }

    @RequestMapping("save")
    public String save(Air_Quility_Index air_quality_index) {
       airService.insertAir(air_quality_index);
        System.out.println(air_quality_index.toString());
        return "redirect:all";
    }
}
